﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Packaging;
using OPCDiffCore;

namespace OPCDiff
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnBrowseOriginal_Click(object sender, EventArgs e)
        {
            openFileDialogOriginal.ShowDialog();
            if (!String.IsNullOrEmpty(openFileDialogOriginal.FileName))
                txtOriginalPath.Text = openFileDialogOriginal.FileName;
        }

        private void btnBrowseTarget_Click(object sender, EventArgs e)
        {
            openFileDialogTarget.ShowDialog();
            if (!String.IsNullOrEmpty(openFileDialogTarget.FileName))
                txtTargetPath.Text = openFileDialogTarget.FileName;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            string openFileDialogFilter =
                "WordprocessingML document (*.docx)|*.docx|Wordprocessing+macro (*.docm)|*.docm|SpreadsheetML document (*.xlsx)|*.xlsx|SpreadsheetML+macro (*.xlsm)|*.xlsm" +
                "|PresentationML document (*.pptx)|*.pptx|XPS document (*.xps)|*.xps";
            openFileDialogOriginal.Filter = openFileDialogFilter;
            openFileDialogOriginal.FileOk += new CancelEventHandler(openFileDialogOriginal_FileOk);

            openFileDialogTarget.Filter = openFileDialogFilter;
            openFileDialogTarget.FileOk += new CancelEventHandler(openFileDialogOriginal_FileOk);
        }

        void openFileDialogOriginal_FileOk(object sender, CancelEventArgs e)
        {
            FileDialog fd = (FileDialog)sender;
            if (String.IsNullOrEmpty(fd.FileName))
                e.Cancel = true;
        }

        private void btnDiff_Click(object sender, EventArgs e)
        {
            // Reset validation actions if any
            txtOriginalPath.BackColor = Color.Empty;
            txtTargetPath.BackColor = Color.Empty;

            // Check files validity(
            if (openFileDialogOriginal.FileName.ToLowerInvariant() == openFileDialogTarget.FileName.ToLowerInvariant())
            {
                txtOriginalPath.Focus();
                txtOriginalPath.BackColor = Color.Red;
                txtTargetPath.BackColor = Color.Red;

                MessageBox.Show("You can't select the same file as original and target document !");
                return;
            }

            if (!File.Exists(openFileDialogOriginal.FileName))
            {
                txtOriginalPath.Focus();
                txtOriginalPath.BackColor = Color.Red;

                MessageBox.Show("You must select a valid file !");
                return;
            }

            if (!File.Exists(openFileDialogTarget.FileName))
            {
                txtTargetPath.Focus();
                txtTargetPath.BackColor = Color.Red;

                MessageBox.Show("You must select a valid file !");
                return;
            }

            OPCDiffer packageComparer = new OPCDiffer();
            using (Package pkgOriginal = Package.Open(openFileDialogOriginal.FileName))
            {
                using (Package pkgDiff = Package.Open(openFileDialogTarget.FileName))
                {
                    if (packageComparer.Equals(pkgOriginal, pkgDiff))
                        MessageBox.Show("Identical files !");
                    else
                        MessageBox.Show("NOT identical files !");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new frmAboutBox().Show();
        }
    }
}
